<?php
App::uses('AppController', 'Controller');
/**
 * CustomersEmails Controller
 *
 * @property CustomersEmail $CustomersEmail
 * @property PaginatorComponent $Paginator
 */
class CustomersEmailsController extends AppController {

/**
 * Components
 *
 * @var array
 */
	
	public $components = array('Paginator');
	// public $components = array('RequestHandler');

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->CustomersEmail->recursive = 0;
		$this->set('customersEmails', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->CustomersEmail->exists($id)) {
			throw new NotFoundException(__('Invalid customers email'));
		}
		$options = array('conditions' => array('CustomersEmail.' . $this->CustomersEmail->primaryKey => $id));
		$this->set('customersEmail', $this->CustomersEmail->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->CustomersEmail->create();
			if ($this->CustomersEmail->save($this->request->data)) {
				$this->Session->setFlash(__('The customers email has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The customers email could not be saved. Please, try again.'));
			}
		}
		$emails = $this->CustomersEmail->Email->find('list');
		$customers = $this->CustomersEmail->Customer->find('list');
		$this->set(compact('emails', 'customers'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->CustomersEmail->exists($id)) {
			throw new NotFoundException(__('Invalid customers email'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->CustomersEmail->save($this->request->data)) {
				$this->Session->setFlash(__('The customers email has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The customers email could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('CustomersEmail.' . $this->CustomersEmail->primaryKey => $id));
			$this->request->data = $this->CustomersEmail->find('first', $options);
		}
		$emails = $this->CustomersEmail->Email->find('list');
		$customers = $this->CustomersEmail->Customer->find('list');
		$this->set(compact('emails', 'customers'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->CustomersEmail->id = $id;
		if (!$this->CustomersEmail->exists()) {
			throw new NotFoundException(__('Invalid customers email'));
		}
		$this->request->allowMethod('post', 'delete');
		if ($this->CustomersEmail->delete()) {
			$this->Session->setFlash(__('The customers email has been deleted.'));
		} else {
			$this->Session->setFlash(__('The customers email could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}

	public function opened($id)
	{
		if (!$this->CustomersEmail->exists($id)) {
			throw new NotFoundException(__('Invalid customers email'));
		}
		$options = array('conditions' => array('CustomersEmail.' . $this->CustomersEmail->primaryKey => $id));
		$this->request->data = $this->CustomersEmail->find('first', $options);

		$this->request->data['CustomersEmail']['opened']=1;
		$this->CustomersEmail->save($this->request->data);
		//var_dump($this->CustomersEmail);

		die();
	}
}
